﻿using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Volo.Abp.Data;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.Guids;
using Volo.Abp.MultiTenancy;
using XfTech.SmartTms.Entities;

namespace XfTech.SmartTms.DataSeed
{
    /// <summary>
    /// 图书模块种子数据
    /// </summary>
    public class BookStoreDataSeedContributor
        : IDataSeedContributor, ITransientDependency
    {
        private readonly IRepository<BookType, Guid> _bookRepository;
        private readonly IGuidGenerator _guidGenerator;
        private readonly ICurrentTenant _currentTenant;

        public BookStoreDataSeedContributor(
            IRepository<BookType, Guid> bookRepository,
            IGuidGenerator guidGenerator,
            ICurrentTenant currentTenant)
        {
            _bookRepository = bookRepository;
            _guidGenerator = guidGenerator;
            _currentTenant = currentTenant;
        }

        public async Task SeedAsync(DataSeedContext context)
        {
            using (_currentTenant.Change(context?.TenantId))
            {
                if (await _bookRepository.GetCountAsync() > 0)
                {
                    return;
                }

                var bookType = new List<BookType>(){
                    new BookType(_guidGenerator.Create(), "计算机应用"),
                    new BookType(_guidGenerator.Create(), "历史类"),
                    new BookType(_guidGenerator.Create(), "哲学类"),
                    };

                await _bookRepository.InsertManyAsync(bookType);
            }
        }
    }
}
